package chartCraft.tableDesk;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JPanel;

import chartCraft.ChartCraftController;
import chartCraft.model.LocationModel;
import chartCraft.model.TopModel;
import chartCraft.model.Model.NotifyingMessage;

public class TableButtonsPane extends JPanel implements Observer {

	private ChartCraftController mChartCraftController;
	private TopModel mModel;

	private JButton addRowBtn;
	private JButton deleteLastRowBtn;
	private JButton addColumnBtn;
	private JButton deleteLastColumnBtn;


	public TableButtonsPane(ChartCraftController chartCraftController,
			TopModel topModel) {
		mChartCraftController = chartCraftController;
		setModel(topModel);

		setLayout(new FlowLayout(FlowLayout.LEADING));
		registerButtons();
	}

	private void registerButtons() {
		addRowBtn = new JButton("Add Row");
		deleteLastRowBtn = new JButton("Delete Last Row");
		addColumnBtn = new JButton("Add Column");
		deleteLastColumnBtn = new JButton("Delete Last Column");

		addRowBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mChartCraftController.insertNewLocation();
			}
		});

		deleteLastRowBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mChartCraftController.deleteLastLocation();
			}
		});

		addColumnBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mChartCraftController.insertNewSeries();
			}
		});

		deleteLastColumnBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mChartCraftController.deleteLastSeries();
			}
		});


		add(addRowBtn);
		add(deleteLastRowBtn);
		add(addColumnBtn);
		add(deleteLastColumnBtn);
	}

	public void setModel(TopModel model) {
		mModel = model;
		mModel.addObserver(this);
		
		reloadFromState();
	}

	private void reloadFromState() {
		for (LocationModel locaitonModel: mModel.getAllLocationModels()) {
			locaitonModel.addObserver(this);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		NotifyingMessage msg = (NotifyingMessage) arg1;
		String action = msg.getAction();

		if (arg0 == mModel) {

			if (action == TopModel.INSERT_NEW_LOCATION_MODEL) {
				LocationModel locationModel = (LocationModel) msg.getObject();
				locationModel.addObserver(this);
				updateButtonsStatus();

			} else if (action == TopModel.DELETE_LOCATION_MODEL) {
				LocationModel locationModel = (LocationModel) msg.getObject();
				locationModel.deleteObserver(this);
				updateButtonsStatus();
			}

		} else if (arg0 instanceof LocationModel) {

			if (action == LocationModel.STUCTURE_CHANGE) {
				updateButtonsStatus();
			}
		}
	}

	private void updateButtonsStatus() {
		if (mModel.getNoOfLocations() <= 1) {
			deleteLastRowBtn.setEnabled(false);
		} else {
			deleteLastRowBtn.setEnabled(true);
		}

		if (mModel.getNoOfSeries() <= 1) {
			deleteLastColumnBtn.setEnabled(false);
		} else {
			deleteLastColumnBtn.setEnabled(true);
		}
	}

}
